<template>
  <div class="dashboard-container">
    <div class="app-container">
      <!-- 详情{{ $route.query.id }} -->
      <el-tabs @tab-click="switchto">
        <el-tab-pane label="登录账户设置">
          <el-form ref="userForm" :model="userInfo" :rules="rules" label-width="120px" style="margin-left: 120px; margin-top:30px">
            <el-form-item prop="username" label="用户名">
              <el-input v-model="userInfo.username" style="width:300px" />
            </el-form-item>
            <el-form-item prop="password2" label="密码">
              <el-input v-model="userInfo.password2" style="width:300px" type="password" />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" size="small" @click="saveUserDetailById">更新</el-button>
              <el-button size="small" @click="$router.back()">取消</el-button>
            </el-form-item>
          </el-form>
        </el-tab-pane>
        <el-tab-pane label="个人详情">
          <el-row type="flex" justify="end">
            <el-tooltip content="打印个人基本信息">
                <!-- <router-link :to="`/employees/print/?id=${userId}?type='personal'`"> -->
                <router-link :to="{name: 'print',params:{type: 'personal'}}">
                  <i class="el-icon-printer" />
                </router-link>
             </el-tooltip>
            </el-row>
          <!-- 动态组件 可以动态切换 -->
          <keep-alive>
            <component :is="componentName" />
          </keep-alive>
        </el-tab-pane>
        <el-tab-pane label="岗位信息">
          <el-row type="flex" justify="end">
              <el-tooltip content="打印岗位信息">
                <!-- <router-link :to="`/employees/print/${userId}?type=job`"> -->
                <router-link :to="{name: 'print',query:{type: 'job'}}">
                  <i class="el-icon-printer"  />
                </router-link>
              </el-tooltip>
          </el-row>
          <!-- 动态组件 可以动态切换 -->
          <!-- keep-alive 给动态组件做缓存 -->
          <keep-alive>
            <component :is="componentName" />
          </keep-alive>
        </el-tab-pane>
      </el-tabs>
    </div>
  </div>
</template>

<script>
import { saveUserDetailById } from "@/api/employees";
import { getUserDetailById } from "@/api/user";
import jobInfo from './components/job-info'
import userInfo from './components/user-info'
export default {
  name: 'detail',
  components: {
    userInfo,
    jobInfo
  },
  props: {  
    id: {
      type: String,
      required: true
    }
  },
  data () {
    return {
      componentName: 'userInfo',
      userInfo: {
         //   专门存放基本信息
        username: '',
        password2: ''
      },
      rules: {
        username: [{ required: true, message: '姓名不能为空', trigger: 'blur' }],
        password2: [{ required: true, message: '密码不能为空', trigger: 'blur' },
          { min: 6, max: 9, message: '密码长度6-9位', trigger: 'blur' }]
      }
    }
  },
  methods: {
    switchto({index}) {
      if (index === '1') {
        this.componentName = 'userInfo'
      } else if (index === '2') {
        this.componentName = 'jobInfo'
      }
    },
    async saveUserDetailById() {
      try {
        await this.$refs.userForm.validate()
        const password = this.userInfo.password2
        // console.log(this.userInfo);
        await saveUserDetailById({ ...this.userInfo, password })
        this.$message.success('修改员工成功')
      } catch (error) {
        console.log(error);
      }
    },
    async getUserDetailById() {
      this.userInfo = await getUserDetailById(this.id)
    }
  },
  created() {
    this.getUserDetailById()
  }
}
</script>

<style>

</style>
